<template>
  <div class="sim-breakpoints">
    <div class="_panel-content">
      <el-table
        :empty-text="$t('no_data')"
        border
        :data="breakpointList"
        height="100%"
        size="small"
        @selection-change="handleSelectionChanged">
        <el-table-column type="selection" width="55" />
        <el-table-column :label="$t('start_using')" width="60">
          <template #default="scope">
            <el-checkbox v-model="scope.row.enabled" size="small" @change="revertEnabled(scope.$index, scope.row.enabled)"/>
          </template>
        </el-table-column>
        <el-table-column :label="$t('mount_element')" width="140">
          <template #default="scope">
            <img
              v-if="scope.row.displayIcon"
              :src="scope.row.displayIcon"
              alt="icon"
              class="_tree-node-icon"
            />
            <span class="node-name">{{scope.row.displayName}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('conditions_precedent')">
          <template #default="scope">
            <el-input
              v-model="scope.row.condition"
              :placeholder="$t('default_is_valid')"
              size="small"
              @change="handleConditionChanged(scope.$index, scope.row.condition)" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('trigger_type')" width="180">
          <template #default="scope">
            <el-select
              v-model="scope.row.trigger"
              class="m-2"
              placeholder="Select"
              size="small"
              @change="handleTriggerChanged(scope.$index, scope.row.trigger)">
              <el-option
                v-for="item in validTrigger"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-table-column>
      </el-table>

    </div>
  </div>
</template>
<script lang="ts">
import { ElTable } from "element-plus";
import { controller, SimulateController } from "@/engine/Controller";
import { BreakpointItem, BreakpointTriggerType } from "@/engine/BreakpointsService";
import { defineComponent, ref } from "vue";

export default defineComponent({
  name: "sim-breakpoints",
  setup() {
    let controllerRef = controller as SimulateController;
    const selectedRows = ref<BreakpointItem[]>([]);
    const validTrigger = [{
      value: BreakpointTriggerType.onEntry,
      label: "onEntry",
    }, {
      value: BreakpointTriggerType.onExit,
      label: "onExit",
    }, {
      value: BreakpointTriggerType.onBoth,
      label: "onBoth",
    }];
    return {
      controllerRef,
      selectedRows,
      validTrigger
    };
  },
  computed: {
    breakpointList() : BreakpointItem[] {
      return this.controllerRef.breakpointsService.breakpointList.map(item => {
        if (item.displayName.includes('undefined')) {
          item.displayName = '';
        }
        return item;

      });
    },
  },
  methods: {
    revertEnabled(index: number, newEnabled: boolean) {
      this.controllerRef.breakpointsService.setEnabledAt(index, newEnabled);
    },
    handleConditionChanged(index: number, newCondition: string) {
      this.controllerRef.breakpointsService.setConditionAt(index, newCondition);
    },
    handleTriggerChanged(index: number, newTrigger: BreakpointTriggerType) {
      this.controllerRef.breakpointsService.setTriggerAt(index, newTrigger);
    },
    handleSelectionChanged(val: BreakpointItem[]) {
      this.selectedRows = val;
    },
    removeSelected() {
      this.controllerRef.breakpointsService.removeBreakpoints(this.selectedRows.map(row => row.modelId));
    },
    refreshFromModel(){
      this.controllerRef.breakpointsService.refreshFromModel();
    },
  },
});
</script>
<style lang="scss">
.sim-breakpoints {
  height: 100%;
  ._panel-content {
    height: 100%;
  }
  .bkp-btns {
    width: 14px;
    height: 14px;
    margin-right: 2px;
    padding-bottom: 2px;
  }
  ._toolbar {
    height: 30px;
    padding-left: 10px;
    border-bottom: 1px solid #DCDCDC;
    display: flex;
    align-items: center;

    ._action-btn {
      line-height: 20px;
      display: inline-flex;
      align-items: center;
      padding: 0 4px;
    }
    .__spacer {
      margin: 0 6px;
      width: 1px;
      height: 16px;
      display: inline-block;
      background: #DCDCDC;
    }
  }
  .el-checkbox.el-checkbox--small {
    height: 24px;
  }
}

</style>
